package com.ms.game.sendgift.db;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;

import com.ms.game.itembox.ChangeItemLister;
import com.ms.game.itembox.Item;
import com.ms.game.itembox.ItemChangeWay;
import com.ms.game.itembox.db.PlayerItemModel;
import com.ms.game.itembox.message.SSimpleItem;
import com.ms.game.lobby.GameLobbyModule;
import com.ms.game.sendgift.GiftPlayerData;
import com.ms.game.sendgift.SendGiftStage;
import com.ms.game.sendgift.message.SendGiftResponse;
import com.ms.player.Player;
import com.xpec.c4.game.common.Helper;
import com.xpec.c4.game.datamanager.BaseDataInfo;
import com.xpec.c4.game.datamanager.agent.money.ChangeCoinLister;
import com.xpec.c4.game.datamanager.agent.money.CoinType;
import com.xpec.c4.game.datamanager.agent.money.MoneyChangeWay;
import com.xpec.c4.game.utility.GUID;
import com.xpec.c4.message.MessageTypes;
import com.xpec.c4.service.module.task.TaskAsyncCommand;

public class SaveGiftNotifyTakenStageAsync extends TaskAsyncCommand {

	/**
	 * @see MessageTypes.S_GIFT_FINIAL_GIVE_RESPONSE
	 */
	public int messageType;
	
	/**
	 * 帳單ID
	 */
	public long orderID;
	
	public GiftPlayerData giftData;
	
	public String playerIP;

	/**
	 * 訂單資料
	 */
	public SendGiftNotifyModel model;
	
	public SendGiftResponse response;
	
	/**
	 * 此流程是否成功
	 */
	public boolean success;
	
	/**
	 * 寄送訂單中的道具資料
	 */
	public ArrayList<PlayerItemModel> sendGiftItems;
	
	private SendGiftRecordLog logModel;
		
	@Override
	public void asyncExecute() throws Exception {
	
		logModel = new SendGiftRecordLog();
		logModel.setOrderID(model.getOrderID());
		logModel.setSendOrderID(model.getSendOrderID());
		logModel.setOrderNumber(model.getOrderNumber());
		logModel.setPlayerGUID(model.getPlayerGUID());
		logModel.setPlayerName(model.getPlayerName());
		logModel.setTargetGUID(model.getTargetGUID());
		logModel.setTargetName(model.getTargetName());

		if (model.getStage() == SendGiftStage.FINAL_TAKEN) {
			logModel.setTargetIP(playerIP);
		} else {
			logModel.setPlayerIP(playerIP);
		}
		logModel.setGoldCoin(model.getGoldCoin());
		logModel.setServiceCharge(model.getServiceCharge());
		logModel.setItems(model.getItems());
		logModel.setStage(model.getStage());
		logModel.setCreateTime(model.getCreateTime());
		logModel.setAnswerTime(model.getAnswerTime());
		logModel.setCompleteTime(model.getFinishTime());
		logModel.setFinalTakenTime(model.getFinalTakenTime());
		if (model.getStage() == SendGiftStage.TIME_OUT_OREDER_TAKEN) {
			logModel.setOutTakenTime(model.getFinalTakenTime());
		}
		logModel.setOverTime(model.getOverTime());
		logModel.setSendbackTime(model.getSendBackTime());
		logModel.setOrderFinishTime(model.getOrderFinishTime());
		logModel.setOverTime(model.getOverTime());
		logModel.setSuccess(success);
		logModel.setFinish(model.isFinish());
		logModel.setTaken(model.isTaken());
		SendGiftRecordDAO.saveOrUpdateGiftNotify(logModel);
		
	}

	/** 
	 * DC callback
	 * {@link GameLobbyModule#sendGiftCallBackFinial(BaseDataInfo, Object...)}
	 */
	@Override
	public void execute() throws Exception {
		
		HashMap<String, SSimpleItem> items = new HashMap<String, SSimpleItem>();
		for(PlayerItemModel itemModel : sendGiftItems){
			Item item = new Item(itemModel);
			items.put(item.getSn(), item.toClientFormat());
		}
		
		Player player = giftData.playerData;
		GameLobbyModule module = giftData.module;
		BigDecimal charge = new BigDecimal(model.getServiceCharge());
		BigDecimal goldCoin = new BigDecimal(model.getGoldCoin());
		MoneyChangeWay moneyChangeWay = null;
		ItemChangeWay itemChangeWay = null;
		int stage = model.getStage();
		BigDecimal coin = BigDecimal.ZERO;
		
		if(stage == SendGiftStage.FINAL_TAKEN){
			//最後一步領取
			coin = goldCoin;
			moneyChangeWay = MoneyChangeWay.ADD_SEND_GIFT;
			itemChangeWay = ItemChangeWay.ADD_SEND_GIFT;
		}else if(stage == SendGiftStage.SENDER_REJECT_TAKEN){
			//自己拒絕 跟 對方拒絕
			coin = Helper.add(goldCoin, charge);
			moneyChangeWay = MoneyChangeWay.ADD_GIFT_RETURN;
			itemChangeWay = ItemChangeWay.ADD_GIFT_RETURN;
		}else if(stage == SendGiftStage.TIME_OUT_OREDER_TAKEN || stage == SendGiftStage.GMTOOL_RETURN_ORDER_TAKEN){
			coin = Helper.add(goldCoin, charge);
			moneyChangeWay = MoneyChangeWay.ADD_GIFT_TIME_OUT_RETURN;
			itemChangeWay = ItemChangeWay.ADD_GIFT_TIME_OUT_RETURN;
		}		
		//設定幣種幣別
		ChangeCoinLister coinLister = new ChangeCoinLister();
		coinLister.addCoin(CoinType.GOLD, coin);
		
		ChangeItemLister itemLister = new ChangeItemLister();
		if (items != null) {
			itemLister.addSendGiftOrderId(model.getOrderID());
			for (PlayerItemModel itemModel : sendGiftItems) {
				itemLister.addItem(itemModel.getSn(), itemModel.getItemId(), itemModel.getCount(), itemModel.getRefineLevel());
			}
		}			
		
		//金錢寫回DC
		module.getPlayerDataAgent().updatePlayerMoneyAndItemToDC(GUID.parseUUID(model.getOrderID()), player, 
				coinLister, moneyChangeWay,
				itemLister, itemChangeWay,
				null, null, null, module, "sendGiftTakenCallBack", 
				messageType, giftData);
	}
}
